//
//  mapping.cpp
//  Steady (Revised Version)
//
//  Created by Duong Ngo on 8/13/16.
//  Copyright © 2016 Duong Ngo. All rights reserved.
//

#include "mapping.hpp"

//-----------------------------------------------------------------------------
// Find the original map f: name of variables --> its position
//-----------------------------------------------------------------------------
void find_original_map (std::map<std::string,int>& f)
{
    int px= 0;
    f["s.gamma"]= px+0; f["s.cb"]=px+1; f["s.Rf"]=px+2; f["s.Rm"]=px+3; f["s.ql"]=px+4;
    f["s.mur"]=px+5; f["s.muc"]=px+6; f["s.ip"]=px+7; f["s.pie"]=px+8;  f["s.n"]=px+9;
    f["s.m"]=px+10; f["s.bh"]=px+11; f["s.k"]=px+12; f["s.tau"]=px+13; f["s.s"]=px+14;
    f["s.ch"]=px+15; f["s.etaz"]=px+16; f["s.lama"]=px+17; f["s.lamb"]=px+18; f["s.etab"]=px+19;
    f["s.i"]=px+20; f["s.pm"]=px+21; f["s.y"]=px+22; f["s.l"]=px+23; f["s.u"]=px+24;
}

//-----------------------------------------------------------------------------
// Find the equation map f: name of equation --> its position
//-----------------------------------------------------------------------------
void find_equation_map (std::map<std::string,int>& f)
{
    //Bankers
    f["B.g"]=0; f["B.rf"]=1; f["B.rm"]=2; f["B.rn"]=3; f["B.ql"]=4; f["B.n"]=5;
    f["B.m"]=6; f["B.rr"]=7; f["B.cr"]=8; f["B.bh"]=9;
    //Households
    f["H.ch"]=10; f["H.lamb"]=11; f["H.rm"]=12; f["H.ql"]=13; f["H.k"]=14; f["H.cia"]=15; f["H.bc"]=16;
    // Firm
    f["F.pi"]=17; f["F.y"]=18;
    // Central bank
    f["C.u"]=19;
    // Other
    f["M.y"]=20; f["M.k"]=21; f["M.ip"]=22; f["C.rf"]=23; f["S.kap"]= 24;
}

//-----------------------------------------------------------------------------
// Find position of state variables and foward variables
//-----------------------------------------------------------------------------
void find_positions_of_state_and_forward_variables (std::vector<int>& state_position, std::vector<int>& forward_position, std::vector<int>& var_role)
{
    
    // State position
    state_position[0]= original_map.at("s.n");
    state_position[1]= original_map.at("s.m");
    state_position[2]= original_map.at("s.bh");
    state_position[3]= original_map.at("s.k");
    state_position[4]= original_map.at("s.Rm");
    
    // Foward position
    forward_position[0]= original_map.at("s.gamma");
    forward_position[1]= original_map.at("s.ip");
    forward_position[2]= original_map.at("s.ql");
    forward_position[3]= original_map.at("s.lamb");
    forward_position[4]= original_map.at("s.lama");
    forward_position[5]= original_map.at("s.pm");
    forward_position[6]= original_map.at("s.pie");
    forward_position[7]= original_map.at("s.y");
    
    // Var role
    for (int i=0; i<var_foc; ++i){
        var_role[i]= dim_state;
    }
    for (int i=0; i<dim_state; ++i){
        var_role[state_position[i]]= i;
    }
}

//-----------------------------------------------------------------------------
// Find the map f: name of variables --> its position
//-----------------------------------------------------------------------------
void find_variable_s_at_time_t (const int& t, const Number* x, variables& s)
{
    // Current Variables
    int px= t*var_foc;
    
    s.gamma= x[px+0]; s.cb= x[px+1]; s.Rf= x[px+2]; s.Rm= x[px+3]; s.ql= x[px+4];
    s.mur= x[px+5]; s.muc= x[px+6];  s.ip= x[px+7]; s.pie= x[px+8]; s.n= x[px+9];
    s.m=x[px+10]; s.bh= x[px+11]; s.k= x[px+12]; s.tau= x[px+13];  s.s=x[px+14];
    s.ch= x[px+15]; s.etaz=x[px+16]; s.lama= x[px+17]; s.lamb=x[px+18]; s.etab= x[px+19];
    s.i=x[px+20]; s.pm=x[px+21]; s.y=x[px+22]; s.l= x[px+23]; s.u= x[px+24];
    
    // Lag Variable
    if (t>0){
        px = (t-1)*var_foc;
        s.n_p= x[px+original_map.at("s.n")];
        s.m_p= x[px+original_map.at("s.m")];
        s.bh_p= x[px+original_map.at("s.bh")];
        s.k_p= x[px+original_map.at("s.k")];
        s.Rm_p= x[px+original_map.at("s.Rm")];
    }
    // Forward variables
    if (t<T){
        px = (t+1)*var_foc;
        s.gamma_f = x[px+ original_map.at("s.gamma")];
        s.ip_f = x[px+ original_map.at("s.ip")];
        s.ql_f = x[px+ original_map.at("s.ql")];
        s.lamb_f = x[px+ original_map.at("s.lamb")];
        s.lama_f = x[px+ original_map.at("s.lama")];
        s.pm_f = x[px+ original_map.at("s.pm")];
        s.pie_f = x[px+ original_map.at("s.pie")];
        s.y_f = x[px+ original_map.at("s.y")];
    }
}

//-----------------------------------------------------------------------------
// Find the map f: name of variables --> its position
//-----------------------------------------------------------------------------
void find_map_at_time_t (const int& t, std::map<std::string,int>& f)
{
    //Current Variables
    int px= t*var_foc;
    f["s.gamma"]= px+0; f["s.cb"]=px+1; f["s.Rf"]=px+2; f["s.Rm"]=px+3; f["s.ql"]=px+4;
    f["s.mur"]=px+5; f["s.muc"]=px+6; f["s.ip"]=px+7; f["s.pie"]=px+8;  f["s.n"]=px+9;
    f["s.m"]=px+10; f["s.bh"]=px+11; f["s.k"]=px+12; f["s.tau"]=px+13; f["s.s"]=px+14;
    f["s.ch"]=px+15; f["s.etaz"]=px+16; f["s.lama"]=px+17; f["s.lamb"]=px+18; f["s.etab"]=px+19;
    f["s.i"]=px+20; f["s.pm"]=px+21; f["s.y"]=px+22; f["s.l"]=px+23; f["s.u"]=px+24;
    
    //Lag variables
    if (t>0){
        px = (t-1)*var_foc;
        f["s.n_p"]= px+original_map.at("s.n");
        f["s.m_p"]= px+original_map.at("s.m");
        f["s.bh_p"]= px+original_map.at("s.bh");
        f["s.k_p"]= px+original_map.at("s.k");
        f["s.Rm_p"]= px+original_map.at("s.Rm");    
    }
    
    //Forward variables
    if (t<T){
        px = (t+1)*var_foc;
        f["s.gamma_f"] = px+ original_map.at("s.gamma");
        f["s.ip_f"] = px+ original_map.at("s.ip");
        f["s.ql_f"] = px+ original_map.at("s.ql");
        f["s.lamb_f"] = px+ original_map.at("s.lamb");
        f["s.lama_f"] = px+ original_map.at("s.lama");
        f["s.pm_f"] = px+ original_map.at("s.pm");
        f["s.pie_f"] = px+ original_map.at("s.pie");
        f["s.y_f"] = px+ original_map.at("s.y");
    }
}





















